﻿using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;

namespace com.fukefu.info.b2b.auto
{
    public class China : Information
    {
        string auth = "", login_name = "";
        //登陆
        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData();
            pd.Add("captcha", vcode);
            pd.Add("destUrl", "");
            pd.Add("op", "B2BLogin");
            pd.Add("userName", name);
            pd.Add("userPassword", pass);
            string html = source.Post("http://my.cn.china.cn/admin.php", pd.Read(), "http://my.cn.china.cn/admin.php?op=LoginShow&destUrl=");
            if (html.IndexOf("href=\"http://my.cn.china.cn/admin.php?op=B2BLogout\">退出</a>") != -1)
            {
                auth = Regex.Match(html, "&auth=([^\"]+)\">我的供应</a>").Groups[1].Value;
                this.login_name = name;
                return true;
            }
            return false;
        }
        //发布一篇文章
        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            InformationResult result = new InformationResult();
            SourceFormData sfd = new SourceFormData("gb2312");
            //发送图片文件
            string picName = RandomPicture.Get();
            sfd.WriteFile("imageToUpload0", picName);
            string html = source.Post("http://my.cn.china.cn/ajax.php?op=uploadFile&canWatertMarker=false&fileFieldName=imageToUpload&picIndex=0", sfd.GetContentType(), sfd.Read(), "http://my.cn.china.cn/admin.php?op=SellInfoPublishShow&auth=" + auth);
            //结束
            ParametersData pd = new ParametersData();
            pd.Add("sellInfoId", "");
            pd.Add("tradeLeadType", "0");
            pd.Add("quantity", "0");
            pd.Add("packaging", "");
            pd.Add("modelNO", "");
            pd.Add("placeOfOrigin", "");
            pd.Add("brandName", "");
            pd.Add("currency", "");
            pd.Add("priceTerms", "");
            pd.Add("otherPriceTerms", "");
            pd.Add("paymentTerms", "");
            pd.Add("paymentRemark", "");
            pd.Add("port", "");
            pd.Add("quality", "");
            pd.Add("offerDetail", content);
            pd.Add("productName", title);
            pd.Add("keyword1", "");
            pd.Add("keyword2", "");
            pd.Add("keyword3", "");
            pd.Add("select", site.TypeId[0]);
            pd.Add("select", site.TypeId[1]);
            pd.Add("categoryName", "");
            pd.Add("categoryId", site.TypeId[1]);
            pd.Add("selectedCate", site.TypeId[1]);
            pd.Add("popDivId", "");
            pd.Add("industryMRU", site.TypeId[0] + "," + site.TypeId[1]);
            pd.Add("industryKW", "");
            pd.Add("categoryType", "SELL");
            pd.Add("needExProp", "1");
            pd.Add("offerSubject", title);
            pd.Add("selectGroup", "");
            pd.Add("newGroupName", "");
            pd.Add("newGroupDesc", "");
            pd.Add("offerDetailHidden", content);
            pd.Add("offerExpire", "90");
            pd.Add("retread", "1");
            pd.Add("picIndex", "0");
            pd.Add("imageToUpload0", picName.Substring(picName.LastIndexOf("\\") + 1));
            pd.Add("watermarker", "on");
            pd.Add("measureUnit", "");
            pd.Add("productPrice", pif.Get("产品价格"));
            pd.Add("minAmount", "1");
            pd.Add("totalAmount", pif.Get("库存数量"));
            pd.Add("deliverDate", "1");
            //发送正文
            html = source.Post("http://my.cn.china.cn/admin.php?op=SellInfoPublish&auth=" + auth, pd.Read(), "http://my.cn.china.cn/admin.php?op=SellInfoPublishShow&auth=" + auth);
            if (html.IndexOf("发布成功，您的信息已提交审核") != -1)
            {
                string uid = "";
                html = source.Get("http://my.cn.china.cn/admin.php?op=SellInfoList&searchType=auditing&auth=" + auth);
                //从审核的里面那网址
                if (html.IndexOf(title) != -1)
                {
                    uid = Regex.Match(html, "&sellInfoId=([0-9]+)&searchType=auditing&auth=" + auth + "\" target=\"_blank\">" + title + "</a>").Groups[1].Value;
                }
                else
                {
                    //从发布成功的里面拿网址
                    html = source.Get("http://my.cn.china.cn/admin.php?op=SellInfoList&auth=" + auth);
                    if (html.IndexOf(title) != -1)
                    {
                        uid = Regex.Match(html, "&sellInfoId=([0-9]+)&searchType=online&auth=" + auth + "\" target=\"_blank\">" + title + "</a>").Groups[1].Value;
                    }
                }
                if (!uid.Equals(""))
                {
                    result.Code = 1;
                    result.Content = "http://detail.cn.china.cn/provide/detail," + uid + ".html|" + "http://" + login_name + ".cn.china.cn/selling-leads/detail," + uid + ".html";
                }
            }
            else
            {
                if (Regex.IsMatch(html, "<div class=\"guideInfo\">(.+?)</div>"))
                {
                    string mess = Regex.Match(html, "<div class=\"guideInfo\">(.+?)</div>").Groups[1].Value.ToString();
                    if (mess.IndexOf("超过限制条数") != -1)
                    {
                        result.Code = 2;
                    }
                }
            }
            return result;
        }
        public override System.Drawing.Image GetFlushCodeImage()
        {
            return source.GetImage("http://my.cn.china.cn/captcha.php?t=2");
        }
    }
}
